import 'package:flutter/material.dart';

// void main() {
//   runApp(const MyApp());
// }
//
// class MyApp extends StatelessWidget {
//   const MyApp({super.key});
//
//   @override
//   Widget build(BuildContext context) {
//     return const MaterialApp(
//       home: AnimatedSwitcherPage(),
//     );
//   }
// }

class AnimatedSwitcherPage extends StatefulWidget {
  const AnimatedSwitcherPage({super.key});

  @override
  State<AnimatedSwitcherPage> createState() => _AnimatedSwitcherPageState();
}

class _AnimatedSwitcherPageState extends State<AnimatedSwitcherPage> {
  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("AnimatedSwitcher 案例")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AnimatedSwitcher(
              duration: const Duration(milliseconds: 500), // 动画时长
              transitionBuilder: (Widget child, Animation<double> animation) {
                // 这里可以自定义切换动画，默认是淡入淡出
                return RotationTransition(turns: animation, child: child);
              },
              child: Text(
                '$_count',
                key: ValueKey<int>(_count), // 关键：子组件必须有不同的 key 才会触发动画
                style: const TextStyle(fontSize: 40),
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              key: ValueKey<String>('mykey'),
              onPressed: () {
                setState(() {
                  _count++;
                });
              },
              child: const Text("加一"),
            ),
          ],
        ),
      ),
    );
  }
}
